package com.wm.controller;

import com.github.pagehelper.PageInfo;
import com.wm.entity.RbacRole;
import com.wm.entity.ResponseResult;
import com.wm.exception.RoleNameExistException;
import com.wm.service.RoleService;
import com.wm.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;


    @GetMapping("/getAll")
    public ResponseResult<List<RbacRole>> getAll(){
        return new ResponseResult<>(roleService.getAll());
    }

    @PostMapping("/add")
    public ResponseResult<Void> insert(@RequestBody @Validated RoleVo roleVo){
        roleService.insert(roleVo.getRole(),roleVo.getPermIds());
        return ResponseResult.SUCCESS;
    }
    @PostMapping("/update")
    public ResponseResult<Void> update(@RequestBody @Validated RoleVo roleVo){
        roleService.UpdateRole(roleVo.getRole(),roleVo.getPermIds());
        return ResponseResult.SUCCESS;
    }

    @PostMapping("/delete")
    public ResponseResult<Void> delete(@RequestBody RoleVo roleVo){

        roleService.deleteId(roleVo.getRole(),roleVo.getPermIds());

        return ResponseResult.SUCCESS;

    }

    @PostMapping("view/{id}")
    public ResponseResult<RbacRole> findRole(@PathVariable int id){
        return new ResponseResult<>(roleService.selectId(id));
    }

    @ExceptionHandler(RoleNameExistException.class)
    public ResponseResult<Void> handleException(){
        return new ResponseResult<>(980,"角色重复");
    }


    @GetMapping("/page/{pageNum}")
    public ResponseResult<PageInfo<RbacRole>> getByPage(@PathVariable int pageNum) {
        return new ResponseResult<>(roleService.getByCondition(pageNum,null));
    }

    //判断角色名是否存在
}
